//
//  EditingViewController.m
//  mid-view
//
//  Created by sus183 on 2020/12/6.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "EditingViewController.h"

@interface EditingViewController ()<UITextViewDelegate>{}

@end

@implementation EditingViewController

@synthesize photoURL = _photoURL;

-(id)init{
    self = [super init];
    if(self){
        leftMargin = 10;
        photoViewWidth = 70;
        photoAreaYcor = 400;
        _photoURL = [NSMutableArray array];
        photoNum = 0;
    }
    return self;
}
-(void)viewDidLoad{
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0,100,self.view.bounds.size.width,400)];
    _textView.delegate = self;
    [_textView setEditable:YES];
    [self.view addSubview:self.textView];
    self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    [self.textView becomeFirstResponder];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"cancel.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = backItem;
    UIBarButtonItem *sendItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"send.png"] style:UIBarButtonItemStylePlain target:self action:@selector(send:)];
    self.navigationItem.rightBarButtonItem = sendItem;
    //self.navigationItem.title = @"添加";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    self.toolView = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.bounds.size.height-50,self.view.bounds.size.width,50)];
    [self.view addSubview:self.toolView];
    UIButton *camera = [[UIButton alloc]initWithFrame:CGRectMake(20,0,35,35)];
    [camera setImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
    [_toolView addSubview:camera];
    [camera addTarget:self action: @selector(selectPhoto) forControlEvents:UIControlEventTouchDown];
    
    _addPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addPhotoButton setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    [_addPhotoButton addTarget:self action: @selector(selectPhoto) forControlEvents:UIControlEventTouchDown];
}

-(void)back:(id)obj{
    [self.navigationController popViewControllerAnimated:YES ];
}

-(void)send:(id)obj{
    
}

-(void)keyboardWillShow:(NSNotification*) aNotification{
    //键盘弹出时 工具栏在键盘上
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    float keyBoardHeight = keyboardRect.size.height;
    [UIView animateWithDuration:0.1 animations:^{
        self.toolView.frame = CGRectMake(0, self.view.bounds.size.height-50-keyBoardHeight, self.view.bounds.size.width, 50);
    }];
}

-(void)keyboardWillHide:(NSNotification* )aNotification{
    [UIView animateWithDuration:0.1 animations:^{
        self.toolView.frame = CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50);
    }];
}

-(void)selectPhoto{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = NO;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

-(void)takePhoto{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

//选择照片完成之后的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{
    //选取完图片后跳转回原控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CGFloat xCoord = leftMargin+ (photoNum % 3)*(photoViewWidth+photoGap);
    CGFloat yCoord = photoAreaYcor+(photoNum / 3)*(photoViewWidth+photoGap);
    photoNum++;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(xCoord,yCoord,photoViewWidth,photoViewWidth)];
    [imageView setImage:image];
    
    if(photoNum == 1){
        [self.view addSubview:imageView];
        _addPhotoButton.frame = CGRectMake((leftMargin + photoViewWidth + photoGap),photoAreaYcor,photoViewWidth, photoViewWidth);
        [self.view addSubview:self.addPhotoButton];
    }else{
        [_addPhotoButton removeFromSuperview];
        [self.view addSubview:imageView];
        _addPhotoButton.frame = CGRectMake((leftMargin + ((photoNum+1) % 3) * (photoViewWidth + photoGap)),(photoAreaYcor+((photoNum + 1) / 3)*(photoViewWidth+photoGap)),photoViewWidth, photoViewWidth);
        [self.view addSubview:self.addPhotoButton];
    }
    
    [_photoURL addObject:[info objectForKey:UIImagePickerControllerImageURL]];
    
    [self.textView becomeFirstResponder];
    //_addPhotoButton.frame = CGRectMake(leftMargin + ((photoNum+1)%3)*(photoViewWidth+photoGap))
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.textView becomeFirstResponder];
}
@end
